package com.drakulo.games.ais.ui.state;

import java.util.ArrayList;
import java.util.List;

import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.Input;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;

import com.drakulo.games.ais.AloneInSpace;
import com.drakulo.games.ais.core.audio.MusicManager;
import com.drakulo.games.ais.ui.I18n;
import com.drakulo.games.ais.ui.ImageManager;
import com.drakulo.games.ais.ui.UIHelper;
import com.drakulo.games.ais.ui.component.ActionHandler;
import com.drakulo.games.ais.ui.component.button.TextButton;
import com.drakulo.games.ais.ui.twlbridge.BasicTWLGameState;

public class MainMenuState extends BasicTWLGameState {
	private static final int menuWidth = 170;
	private static final int menuPadding = 10;
	private static final int menuX = 200;
	private static final int menuY = 200;

	private int id;
	private GameContainer container;
	private StateBasedGame game;

	private Image background;

	private TextButton exitButton;
	private TextButton optionsButton;
	private TextButton newGameButton;

	private List<TextButton> buttons;

	private String bgMusic;
	private boolean started;

	public MainMenuState(int id) {
		this.id = id;

		buttons = new ArrayList<TextButton>();

		final int bwidth = 150;
		final int bheight = TextButton.DEFAULT_HEIGHT;
		newGameButton = new TextButton(I18n.getFirstToUpper("global.new_game"));
		newGameButton.setSize(bwidth, bheight);
		newGameButton.setActionHandler(new ActionHandler() {
			public void run() {
				game.enterState(AloneInSpace.SECTOR_STATE);
			}
		});
		getRootPane().add(newGameButton.getBindable());
		buttons.add(newGameButton);

		optionsButton = new TextButton(I18n.getFirstToUpper("global.settings"));
		optionsButton.setSize(bwidth, bheight);
		optionsButton.disable();
		optionsButton.setActionHandler(new ActionHandler() {
			public void run() {
				// TODO
			}
		});
		getRootPane().add(optionsButton.getBindable());
		buttons.add(optionsButton);

		exitButton = new TextButton(I18n.getFirstToUpper("global.exit"));
		exitButton.setSize(bwidth, bheight);
		exitButton.setActionHandler(new ActionHandler() {
			public void run() {
				container.exit();
			}
		});
		getRootPane().add(exitButton.getBindable());
		buttons.add(exitButton);

		int dx = menuX + menuPadding;
		int dy = menuY + menuPadding;
		final int padd = TextButton.DEFAULT_HEIGHT + menuPadding/2;

		for (TextButton tb : buttons) {
			tb.setPosition(dx, dy);
			dy += padd;
		}
	}

	@Override
	public void init(GameContainer container, StateBasedGame game)
			throws SlickException {
		background = ImageManager.getGfx("space_background");
		this.container = container;
		this.game = game;
		bgMusic = MusicManager.M1;
		started = false;
	}

	@Override
	public void render(GameContainer arg0, StateBasedGame arg1, Graphics g)
			throws SlickException {
		this.background.draw();

		final int menuHeight = buttons.size() * TextButton.DEFAULT_HEIGHT
				+ (buttons.size()) * menuPadding;
		UIHelper.drawWindow(g, menuX, menuY, menuWidth, menuHeight);
		for (TextButton tb : buttons) {
			tb.render(g);
		}
	}

	@Override
	public void update(GameContainer container, StateBasedGame game, int arg2)
			throws SlickException {
		MusicManager musicManager = MusicManager.getInstance();
		if (!started || musicManager.getPlayingMusic() == null) {
			started = true;
			musicManager.playMusic(bgMusic);
		}

		Input input = container.getInput();
		for (TextButton tb : buttons) {
			tb.update(input);
		}
	}

	@Override
	public int getID() {
		return id;
	}

}
